<?php
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 2019/3/31
 * Time: 下午4:32
 */

namespace App\Http\Controllers\Api\Hotel;


use App\Http\Controllers\ApiController;
use App\Models\Hotel;
use App\Models\HotelOwnerApply;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class HotelOwnerController extends ApiController
{
    /**酒店入住申请
     * @param Request $request
     */
    public function api_apply(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'hotel_open_id' => 'required',
            'phone' => 'required',
            'true_name' => 'required',
            'hotel_id' => 'required',
        ]);

        if($validator->fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }

        if(!HotelOwnerApply::do_apply($request->all())){
           return self::response(null,self::ERROR_CODE,HotelOwnerApply::$error);
        }

        return self::response(null,self::SUC_CODE,'您已成功提交申请');


    }

    /**酒店列表
     * @param Request $request
     */
    public function hotel_list(Request $request)
    {
        return self::response(Hotel::list(),self::SUC_CODE);
    }

    /**
     * 检查该酒店管理员是否已经入驻至少一家酒店
     */
    public function detect_owner(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'hotel_open_id' => 'required',
        ]);
        if($validator->fails()){
            return self::response(null,self::ERROR_CODE,$validator);
        }
        $res = HotelOwnerApply::is_valid_hotel_owner($request->input('hotel_open_id'));
        if($res){
            return self::response(null,self::SUC_CODE,'该商家已入驻酒店');
        }
        return self::response(null,self::ERROR_CODE,'该商家未入驻');
    }
}